package com.ruoyi.business.device.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.device.mapper.DeviceMapper;
import com.ruoyi.business.device.domain.Device;
import com.ruoyi.business.device.service.IDeviceService;

/**
 * 设备信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class DeviceServiceImpl implements IDeviceService 
{
    @Autowired
    private DeviceMapper deviceMapper;

    /**
     * 查询设备信息
     * 
     * @param deviceId 设备信息主键
     * @return 设备信息
     */
    @Override
    public Device selectDeviceByDeviceId(Long deviceId)
    {
        return deviceMapper.selectDeviceByDeviceId(deviceId);
    }

    /**
     * 查询设备信息列表
     * 
     * @param device 设备信息
     * @return 设备信息
     */
    @Override
    public List<Device> selectDeviceList(Device device)
    {
        return deviceMapper.selectDeviceList(device);
    }

    /**
     * 新增设备信息
     * 
     * @param device 设备信息
     * @return 结果
     */
    @Override
    public int insertDevice(Device device)
    {
        device.setCreateTime(DateUtils.getNowDate());
        return deviceMapper.insertDevice(device);
    }

    /**
     * 修改设备信息
     * 
     * @param device 设备信息
     * @return 结果
     */
    @Override
    public int updateDevice(Device device)
    {
        device.setUpdateTime(DateUtils.getNowDate());
        return deviceMapper.updateDevice(device);
    }

    /**
     * 批量删除设备信息
     * 
     * @param deviceIds 需要删除的设备信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceByDeviceIds(Long[] deviceIds)
    {
        return deviceMapper.deleteDeviceByDeviceIds(deviceIds);
    }

    /**
     * 删除设备信息信息
     * 
     * @param deviceId 设备信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceByDeviceId(Long deviceId)
    {
        return deviceMapper.deleteDeviceByDeviceId(deviceId);
    }

    /**
     * 根据设备中继编码查询设备信息
     * 
     * @param deviceRelayCode 设备中继编码
     * @return 设备信息
     */
    @Override
    public Device selectDeviceByDeviceRelayCode(String deviceRelayCode)
    {
        return deviceMapper.selectDeviceByDeviceRelayCode(deviceRelayCode);
    }

    /**
     * 校验设备中继编码是否唯一
     * 
     * @param device 设备信息
     * @return 结果
     */
    @Override
    public String checkDeviceRelayCodeUnique(Device device)
    {
        Long deviceId = StringUtils.isNull(device.getDeviceId()) ? -1L : device.getDeviceId();
        Device info = deviceMapper.checkDeviceRelayCodeUnique(device.getDeviceRelayCode(), deviceId);
        if (StringUtils.isNotNull(info) && info.getDeviceId().longValue() != deviceId.longValue())
        {
            return "1";
        }
        return "0";
    }

    /**
     * 更新设备连接状态
     * 
     * @param deviceId 设备ID
     * @param connectionStatus 连接状态
     * @return 结果
     */
    @Override
    public int updateDeviceConnectionStatus(Long deviceId, String connectionStatus)
    {
        Device device = new Device();
        device.setDeviceId(deviceId);
        device.setConnectionStatus(connectionStatus);
        device.setUpdateTime(DateUtils.getNowDate());
        return deviceMapper.updateDevice(device);
    }

    /**
     * 更新设备最后通信时间
     * 
     * @param deviceId 设备ID
     * @return 结果
     */
    @Override
    public int updateDeviceLastCommTime(Long deviceId)
    {
        Device device = new Device();
        device.setDeviceId(deviceId);
        device.setLastCommTime(DateUtils.getNowDate());
        device.setUpdateTime(DateUtils.getNowDate());
        return deviceMapper.updateDevice(device);
    }
}
